package com.lzh.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author zhehen.lu
 * @date 2025/8/13 19:59
 */
public class JacksonBigDecimalLtZeroNullOrIntegerSerializer extends JsonSerializer<BigDecimal> {
    public JacksonBigDecimalLtZeroNullOrIntegerSerializer() {
    }

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (bigDecimal == null) {
            jsonGenerator.writeNumber(0);
        } else if (bigDecimal.compareTo(new BigDecimal("0")) < 0) {
            jsonGenerator.writeNumber((Integer)null);
        } else {
            jsonGenerator.writeNumber(bigDecimal.intValue());
        }

    }
}
